/*
* $Id: SettingsManager.cpp 143 2010-09-09 19:55:18Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "InterfaceStn.h"
#include "SettingsManager.h"
#include "MainSettings.h"

#include "Common/RefObject.h"
CRefObject<CSettingsManager> g_SettingsManager;
ISettingsManager* ISettingsManager::GetSettingsManager(){
	return g_SettingsManager.GetObject();
}

CSettingsManager::Compare::Compare() : lastString(_T("")){}
bool CSettingsManager::Compare::operator()(const std::tstring& first, const std::tstring& second) const{
	bool bfirst;
	if(second == lastString)
		bfirst = false;
	else
		bfirst = true;
	*(std::tstring*)&lastString = first;
	return bfirst;
}
CSettingsManager::CSettingsManager(){}
CSettingsManager::~CSettingsManager(){}

void CSettingsManager::Init(){
	std::vector<IComponent*> aComponents = IApplication::GetApp()->GetComponents();
	for(std::vector<IComponent*>::iterator it = aComponents.begin(); it != aComponents.end(); it++){
		m_aSettings.insert(m_aSettings.end(), std::make_pair((*it)->GetNameComponent(), (*it)->GetSettings()));
	}
}

std::vector<ISetting*> CSettingsManager::GetSettings(){
	std::vector<ISetting*> aSettings;
	for(std::map<std::tstring, std::vector<ISetting*>,Compare>::iterator it = m_aSettings.begin(); it != m_aSettings.end(); it++){
		aSettings.insert(aSettings.end(), it->second.begin(), it->second.end());
	}
	return aSettings;
}

std::vector<ISetting*> CSettingsManager::GetSettings(const std::tstring& strComponentName){
	std::map<std::tstring, std::vector<ISetting*>, Compare>::iterator it = m_aSettings.find(strComponentName);
	return it->second;
}

ISetting* CSettingsManager::GetSetting(const std::tstring& strSettingName){
	for(std::map<std::tstring, std::vector<ISetting*>, Compare>::iterator it = m_aSettings.begin(); it != m_aSettings.end(); it++){
		for(std::vector<ISetting*>::iterator it1 = it->second.begin(); it1 != it->second.end(); it1++){
			if((*it1)->GetNameSetting() == strSettingName){
				return *it1;}
		}
	}
	return NULL;
}
